<?php
/**
 * Crawler
 *
 *     SCMwiki Copyright (C) 2010  Callan Bryant <callan1990@googlemail.com>
 *
 *     Based on Voswork - A simple, fast PHP filesystem abstraction layer
 *     Voswork Copyright (C) 2010  Callan Bryant <callan1990@googlemail.com>
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 * @package main
 * @author Callan Bryant <callan1990@googlemail.com>
 */

/**
 * crawler class - parses all wiki pages into articles to cache
 * 
 *
 * @package core
 * @author Callan Bryant
 */
class scmwikiCrawler //implements crawler
{
	static protected $indexer = null;
	/**
	 * returns the corresposnding article given a page NAME
	 *
	 * @param string $page name of wiki page
	 * @param should it update the 'featured' array?
	 * @return object article
	 */
	public static function get_article($path)
	{
		if (!file_exists($path))
		{
			// invalidate any cache of the article, it does not
			// exist any more
			kernel::$cache->delete('article'.$path);
			return null;
		}				

		// load pre-parsed article from cache
		// or instantiate the parser, and make it parse the page
		// into a new article, then cache it.
		// the article is always up to date magically!!!
		$article = kernel::$cache->get('article'.$path);
		if (!$article or !$article->is_current() )
		{
			// cache miss or cached article is outdated...
			$article = new parser($path);
			kernel::$cache->set('article'.$path,$article);
			self::getIndexer()->add($path,$article->tags);
		}

		return $article;
	}

	// TODO: fix this
	public static function getIndexer()
	{
/*
		if (self::$indexer)
			return $indexer;
		else
			return self::$indexer = new keyStoreIndexer();
*/
return new keyStoreIndexer();
	}

	public static function crawl()
	{
		// ensure manifest is up-to-date by recreating it
		// so that new files will always be listed
		$paths = new manifest(WIKI_FILE_REGEX);
		$paths->create();

		foreach ($paths->dump() as $path)
		{
			// reset timeout per file (10 secs to allow for anything
			// after. Future *compiler* may use a lot of time)
			set_time_limit(10);

			// parse if necessary (and hence automatically cache)
			$article = self::get_article($path);
		}

		return count($paths->dump());
	}
}
